home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / ede_enc.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  5KB  |  164 lines

  1. /* lib/des/ede_enc.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include "des_locl.h"
  49.  
  50. void des_ede3_cbc_encrypt(input, output, length, ks1, ks2, ks3, ivec, encrypt)
  51. des_cblock (*input);
  52. des_cblock (*output);
  53. long length;
  54. des_key_schedule ks1;
  55. des_key_schedule ks2;
  56. des_key_schedule ks3;
  57. des_cblock (*ivec);
  58. int encrypt;
  59.     {
  60.     register unsigned long tin0,tin1;
  61.     register unsigned long tout0,tout1,xor0,xor1;
  62.     register unsigned char *in,*out;
  63.     register long l=length;
  64.     unsigned long tin[2];
  65.     unsigned char *iv;
  66.  
  67.     in=(unsigned char *)input;
  68.     out=(unsigned char *)output;
  69.     iv=(unsigned char *)ivec;
  70.  
  71.     if (encrypt)
  72.         {
  73.         c2l(iv,tout0);
  74.         c2l(iv,tout1);
  75.         for (; l>0; l-=8)
  76.             {
  77.             if (l >= 8)
  78.                 {
  79.                 c2l(in,tin0);
  80.                 c2l(in,tin1);
  81.                 }
  82.             else
  83.                 c2ln(in,tin0,tin1,l);
  84.             tin0^=tout0;
  85.             tin1^=tout1;
  86.  
  87.             IP(tin0,tin1);
  88.  
  89.             tin[0]=tin0;
  90.             tin[1]=tin1;
  91.             des_encrypt2((unsigned long *)tin,ks1,DES_ENCRYPT);
  92.             des_encrypt2((unsigned long *)tin,ks2,DES_DECRYPT);
  93.             des_encrypt2((unsigned long *)tin,ks3,DES_ENCRYPT);
  94.             tout0=tin[0];
  95.             tout1=tin[1];
  96.  
  97.             FP(tout1,tout0);
  98.  
  99.             l2c(tout0,out);
  100.             l2c(tout1,out);
  101.             }
  102.         iv=(unsigned char *)ivec;
  103.         l2c(tout0,iv);
  104.         l2c(tout1,iv);
  105.         }
  106.     else
  107.         {
  108.         c2l(iv,xor0);
  109.         c2l(iv,xor1);
  110.         for (; l>0; l-=8)
  111.             {
  112.             register unsigned long t0,t1;
  113.  
  114.             c2l(in,tin0);
  115.             c2l(in,tin1);
  116.  
  117.             t0=tin0;
  118.             t1=tin1;
  119.             IP(tin0,tin1);  
  120.  
  121.             tin[0]=tin0;
  122.             tin[1]=tin1;
  123.             des_encrypt2((unsigned long *)tin,ks3,DES_DECRYPT);
  124.             des_encrypt2((unsigned long *)tin,ks2,DES_ENCRYPT);
  125.             des_encrypt2((unsigned long *)tin,ks1,DES_DECRYPT);
  126.             tout0=tin[0];
  127.             tout1=tin[1];
  128.  
  129.             FP(tout1,tout0); /**/
  130.  
  131.             tout0^=xor0;
  132.             tout1^=xor1;
  133.             if (l >= 8)
  134.                 {
  135.                 l2c(tout0,out);
  136.                 l2c(tout1,out);
  137.                 }
  138.             else
  139.                 l2cn(tout0,tout1,out,l);
  140.             xor0=t0;
  141.             xor1=t1;
  142.             }
  143.         iv=(unsigned char *)ivec;
  144.         l2c(xor0,iv);
  145.         l2c(xor1,iv);
  146.         }
  147.     tin0=tin1=tout0=tout1=xor0=xor1=0;
  148.     tin[0]=tin[1]=0;
  149.     }
  150.  
  151. #ifdef undef /* MACRO */
  152. void des_ede2_cbc_encrypt(input, output, length, ks1, ks2, ivec, enc)
  153. des_cblock (*input);
  154. des_cblock (*output);
  155. long length;
  156. des_key_schedule ks1;
  157. des_key_schedule ks2;
  158. des_cblock (*ivec);
  159. int enc;
  160.     {
  161.     des_ede3_cbc_encrypt(input,output,length,ks1,ks2,ks1,ivec,enc);
  162.     }
  163. #endif
  164.